﻿#include "mainwindow.h"
#include "addclass.h"
#include "addschool.h"
#include "addstudent.h"
#include "classdata.h"
#include "schooldata.h"
#include "studentdata.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QList>
#include <QMessageBox>
#include <QTableWidgetItem>
#include <QTreeWidgetItem>

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_schoolList.clear();

    SchoolData* mySchoolData = new SchoolData;
    mySchoolData->setSchoolName(QStringLiteral("清华大学"));
    m_schoolList.append(mySchoolData);

    ClassData* myClassData = new ClassData;
    myClassData->setClassName(QStringLiteral("1班"));
    m_schoolList.at(0)->appendClassList(myClassData);

    StudentData* myStudentData = new StudentData;
    myStudentData->setStuName(QStringLiteral("张三"));
    myStudentData->setStuNum(QStringLiteral("1002"));
    myStudentData->setStuSex(QStringLiteral("男"));
    myStudentData->setStuBirthday(QStringLiteral("1999年04月17日"));
    myStudentData->setStuHigh(QStringLiteral("175"));
    mySchoolData->setSchoolName(QStringLiteral("清华大学"));
    m_schoolList.at(0)->getClassList().at(0)->appendStudentList(myStudentData);

    QTreeWidgetItem* schoolItem = new QTreeWidgetItem(QStringList() << m_schoolList.at(0)->getSchoolName());
    ui->treeWidget->addTopLevelItem(schoolItem);

    QTreeWidgetItem* classItem = new QTreeWidgetItem(QStringList() << m_schoolList.at(0)->getClassList().at(0)->getClassName());
    schoolItem->addChild(classItem);

    QTreeWidgetItem* studentItem = new QTreeWidgetItem(QStringList() << m_schoolList.at(0)->getClassList().at(0)->getStudentList().at(0)->getStuName());
    classItem->addChild(studentItem);

    connect(ui->treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(treeDoubleClicked(QTreeWidgetItem*)));

    ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(RightClicked(QPoint)));

    connect(ui->addSchoolAction, &QAction::triggered, this, [=]() {
        addSchool();
    });

    connect(ui->delSchoolAction, &QAction::triggered, this, [=]() {
        delSchool();
    });

    connect(ui->addClassAction, &QAction::triggered, this, [=]() {
        addClass();
    });

    connect(ui->delClassAction, &QAction::triggered, this, [=]() {
        delClass();
    });

    connect(ui->addStudentAction, &QAction::triggered, this, [=]() {
        addStudent();
    });

    connect(ui->delStudentAction, &QAction::triggered, this, [=]() {
        delStudent();
    });

    connect(ui->searchPushButton, &QPushButton::clicked, this, [=]() {
        QString text = ui->searchLineEdit->text();
        searchStudent(text);
        if (!searchStudent(text)) {
            QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("查无此人!"));
        }
    });
}

bool MainWindow::searchStudent(QString text)
{
    for (int i = ui->tableWidget->rowCount(); i > 0; i--) {
        ui->tableWidget->removeRow(0);
    }
    if (text.isEmpty() == true) {
        for (int i = 0; i < m_schoolList.count(); i++) {
            for (int j = 0; j < m_schoolList.at(i)->getClassList().count(); j++) {
                for (int k = 0; k < m_schoolList.at(i)->getClassList().at(j)->getStudentList().count(); k++) {
                    ui->tableWidget->insertRow(k);
                    ui->tableWidget->setItem(k, 0, new QTableWidgetItem(m_schoolList.at(i)->getClassList().at(j)->getStudentList().at(k)->getStuName()));
                    ui->tableWidget->setItem(k, 1, new QTableWidgetItem(m_schoolList.at(i)->getClassList().at(j)->getStudentList().at(k)->getStuNum()));
                    ui->tableWidget->setItem(k, 2, new QTableWidgetItem(m_schoolList.at(i)->getClassList().at(j)->getStudentList().at(k)->getStuSex()));
                    ui->tableWidget->setItem(k, 3, new QTableWidgetItem(m_schoolList.at(i)->getClassList().at(j)->getStudentList().at(k)->getStuBirthday()));
                    ui->tableWidget->setItem(k, 4, new QTableWidgetItem(m_schoolList.at(i)->getClassList().at(j)->getStudentList().at(k)->getStuHigh()));
                    for (int l = 0; l < 5; l++) {
                        ui->tableWidget->item(k, l)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
                    }
                }
            }
        }
        return true;
    } else {
        for (int i = 0; i < m_schoolList.count(); i++) {
            for (int j = 0; j < m_schoolList.at(i)->getClassList().count(); j++) {
                for (int k = 0; k < m_schoolList.at(i)->getClassList().at(j)->getStudentList().count(); k++) {
                    if (m_schoolList.at(i)->getClassList().at(j)->getStudentList().at(k)->getStuNum() == text) {
                        ui->tableWidget->insertRow(0);
                        ui->tableWidget->setItem(0, 0, new QTableWidgetItem(m_schoolList.at(i)->getClassList().at(j)->getStudentList().at(k)->getStuName()));
                        ui->tableWidget->setItem(0, 1, new QTableWidgetItem(m_schoolList.at(i)->getClassList().at(j)->getStudentList().at(k)->getStuNum()));
                        ui->tableWidget->setItem(0, 2, new QTableWidgetItem(m_schoolList.at(i)->getClassList().at(j)->getStudentList().at(k)->getStuSex()));
                        ui->tableWidget->setItem(0, 3, new QTableWidgetItem(m_schoolList.at(i)->getClassList().at(j)->getStudentList().at(k)->getStuBirthday()));
                        ui->tableWidget->setItem(0, 4, new QTableWidgetItem(m_schoolList.at(i)->getClassList().at(j)->getStudentList().at(k)->getStuHigh()));
                        for (int l = 0; l < 5; l++) {
                            ui->tableWidget->item(0, l)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
                        }
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

void MainWindow::treeDoubleClicked(QTreeWidgetItem* item)
{
    ui->treeWidget->sortByColumn(0, Qt::AscendingOrder);
    ui->treeWidget->setSortingEnabled(true);
    item = ui->treeWidget->currentItem();
    if (item->parent() == nullptr) {
        return;
    } else if (item->parent() != nullptr && item->parent()->parent() == nullptr) {

        for (int i = ui->tableWidget->rowCount(); i > 0; i--) {
            ui->tableWidget->removeRow(0);
        }

        int schoolIndex = searchSchool(item->parent());
        QList<ClassData*> myClassList = m_schoolList.at(schoolIndex)->getClassList();
        int classIndex = searchClassInSchool(item, schoolIndex);
        QList<StudentData*> myStudentList = myClassList.at(classIndex)->getStudentList();
        for (int i = 0; i < myStudentList.count(); i++) {
            ui->tableWidget->insertRow(i);
            ui->tableWidget->setItem(i, 0, new QTableWidgetItem(myStudentList.at(i)->getStuName()));
            ui->tableWidget->setItem(i, 1, new QTableWidgetItem(myStudentList.at(i)->getStuNum()));
            ui->tableWidget->setItem(i, 2, new QTableWidgetItem(myStudentList.at(i)->getStuSex()));
            ui->tableWidget->setItem(i, 3, new QTableWidgetItem(myStudentList.at(i)->getStuBirthday()));
            ui->tableWidget->setItem(i, 4, new QTableWidgetItem(myStudentList.at(i)->getStuHigh()));
            for (int j = 0; j < 5; j++) {
                ui->tableWidget->item(i, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
            }
        }
    } else {
        return;
    }
}

void MainWindow::RightClicked(const QPoint& pos)
{
    QTreeWidgetItem* item = ui->treeWidget->itemAt(pos);
    QMenu rightMent(ui->treeWidget);
    if (item == nullptr) {
        rightMent.addAction(ui->addSchoolAction);
    } else if (item->parent() == nullptr) {
        rightMent.addAction(ui->delSchoolAction);
        rightMent.addAction(ui->addClassAction);
    } else if (item->parent() != nullptr && item->parent()->parent() == nullptr) {
        rightMent.addAction(ui->delClassAction);
        rightMent.addAction(ui->addStudentAction);
    } else if (item->parent()->parent() != nullptr) {
        rightMent.addAction(ui->delStudentAction);
    } else {
        return;
    }
    rightMent.exec(QCursor::pos());
}

void MainWindow::addSchool()
{
    AddSchool* myAddSchool = new AddSchool;
    myAddSchool->exec();
    for (int i = 0; i < m_schoolList.count(); i++) {
        if (myAddSchool->getSchoolName() == m_schoolList.at(i)->getSchoolName()) {
            QMessageBox::warning(this, QStringLiteral("警告"), QStringLiteral("该学校已存在,请核对后添加!"));
            return;
        }
    }
    SchoolData* mySchoolData = new SchoolData;
    mySchoolData->setSchoolName(myAddSchool->getSchoolName());
    m_schoolList.append(mySchoolData);
    QTreeWidgetItem* updateItem = new QTreeWidgetItem(QStringList() << myAddSchool->getSchoolName());
    ui->treeWidget->addTopLevelItem(updateItem);
}

void MainWindow::delSchool()
{
    QTreeWidgetItem* item = ui->treeWidget->currentItem();
    int schoolIndex = searchSchool(item);
    m_schoolList.removeAt(schoolIndex);
    delete ui->treeWidget->topLevelItem(ui->treeWidget->currentIndex().row());
}

void MainWindow::addClass()
{
    AddClass* myAddClass = new AddClass;
    myAddClass->exec();
    QTreeWidgetItem* item = ui->treeWidget->currentItem();
    int schoolIndex = searchSchool(item);
    QList<ClassData*> myClassList = m_schoolList.at(schoolIndex)->getClassList();
    for (int i = 0; i < myClassList.count(); i++) {
        if (myAddClass->getClassName() == myClassList.at(i)->getClassName()) {
            QMessageBox::warning(this, QStringLiteral("警告"), QStringLiteral("该班级已存在,请核对后添加!"));
            return;
        }
    }

    ClassData* myClassData = new ClassData;
    myClassData->setClassName(myAddClass->getClassName());
    m_schoolList.at(schoolIndex)->appendClassList(myClassData);
    QTreeWidgetItem* updateItem = new QTreeWidgetItem(QStringList() << myAddClass->getClassName());
    item->addChild(updateItem);
}

void MainWindow::delClass()
{
    QTreeWidgetItem* item = ui->treeWidget->currentItem();
    int schoolIndex = searchSchool(item->parent());
    int classIndex = searchClassInSchool(item, schoolIndex);
    m_schoolList.at(schoolIndex)->removeClassList(classIndex);
    delete item->parent()->takeChild(ui->treeWidget->currentIndex().row());
}

void MainWindow::addStudent()
{
    AddStudent* myAddStudent = new AddStudent;
    myAddStudent->exec();
    QTreeWidgetItem* item = ui->treeWidget->currentItem();
    if (myAddStudent->getStuName().isEmpty() == true) {

        return;
    }
    int schoolIndex = searchSchool(item->parent());
    QList<ClassData*> myClassList = m_schoolList.at(schoolIndex)->getClassList();
    int classIndex = searchClassInSchool(item, schoolIndex);
    QList<StudentData*> myStudentList = myClassList.at(classIndex)->getStudentList();
    for (int i = 0; i < myStudentList.count(); i++) {
        if (myAddStudent->getStuNum() == myStudentList.at(i)->getStuNum()) {
            QMessageBox::warning(this, QStringLiteral("警告"), QStringLiteral("该学号已存在,请核对后添加!"));
            return;
        }
    }
    StudentData* myStudentData = new StudentData;
    myStudentData->setStuName(myAddStudent->getStuName());
    myStudentData->setStuNum(myAddStudent->getStuNum());
    myStudentData->setStuSex(myAddStudent->getStuSex());
    myStudentData->setStuBirthday(myAddStudent->getStuBirthday());
    myStudentData->setStuHigh(myAddStudent->getStuHigh());
    myClassList.at(classIndex)->appendStudentList(myStudentData);
    treeDoubleClicked(item);
    QTreeWidgetItem* updatedItem = new QTreeWidgetItem(QStringList() << myAddStudent->getStuName());
    item->addChild(updatedItem);
}

void MainWindow::delStudent()
{

    QTreeWidgetItem* item = ui->treeWidget->currentItem();
    QString stuName = ui->treeWidget->currentItem()->text(0);
    int schoolIndex = searchSchool(item->parent()->parent());
    QList<ClassData*> myClassList = m_schoolList.at(schoolIndex)->getClassList();
    int classIndex = searchClassInSchool(item->parent(), schoolIndex);
    QList<StudentData*> myStudentList = myClassList.at(classIndex)->getStudentList();
    for (int i = 0; i < myStudentList.count(); i++) {
        if (stuName == myStudentList.at(i)->getStuName()) {
            myClassList.at(classIndex)->removeStudentList(i);
        }
    }
    delete item->parent()->takeChild(ui->treeWidget->currentIndex().row());
    ui->tableWidget->removeRow(ui->treeWidget->currentIndex().row());
    treeDoubleClicked(item);
}

int MainWindow::searchSchool(QTreeWidgetItem* item)
{
    int schoolIndex = 0;
    for (; schoolIndex < m_schoolList.count(); schoolIndex++) {
        if (m_schoolList.at(schoolIndex)->getSchoolName() == item->text(0)) {
            break;
        }
    }
    return schoolIndex;
}

int MainWindow::searchClassInSchool(QTreeWidgetItem* item, int schoolIndex)
{
    QList<ClassData*> myClassList = m_schoolList.at(schoolIndex)->getClassList();
    int classIndex = 0;
    for (; classIndex < myClassList.count(); classIndex++) {
        if (myClassList.at(classIndex)->getClassName() == item->text(0)) {
            break;
        }
    }
    return classIndex;
}

MainWindow::~MainWindow()
{
    delete ui;
}
